/*
 * Created on 2021年11月29日
 * 
 * Copyright(c) YuZhou Big Data Laboratory CO.,Ltd, 2022.  All rights reserved.
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/
package org.pentaho.di.ui.spoon.cluster;

import java.net.URLEncoder;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Table;
import org.pentaho.di.cluster.SlaveServer;
import org.yzbdl.lanius.core.quartz.entity.TaskCronInfo;
import org.pentaho.di.i18n.BaseMessages;
import org.pentaho.di.ui.spoon.Spoon;

/**
 * 统一管理右键菜单，编写Action Group
 * 
 * @author yzbdl@yzbdl.ac.cn
 * @version 1.0
 */
public class MyActionGroup {
    private TableViewer tableViewer;

    private SlaveServer slaveServer;

    /**
     * 鼠标右键有菜单,首先要 生成菜单Menu,并将两个Action传入
     * 
     * @param mgr
     *            菜单管理类
     */
    public void fillContextMenu(IMenuManager mgr) {
        // 加入两个Action对象到菜单管理器中
        MenuManager menuManager = (MenuManager)mgr; // 因为传入的是一个接口,所以这个地方要转换一下类名.
        menuManager.add(new OpenAction());
        menuManager.add(new RefreshAction());

        // 生成Menu并挂载表格table上,menu和table两个对象互为对方的参数.
        Table table = tableViewer.getTable();
        Menu menu = menuManager.createContextMenu(table);
        table.setMenu(menu);

    }

    /**
     * 用来接受TableViewer对象的构造函数。 因为在Action会要使用到TableViewer对象 所以一定要把TableViewer传进来。
     * 
     * @param tableViewer
     *            表格对象
     */
    public MyActionGroup(TableViewer tableViewer, SlaveServer slaveServer) {
        this.tableViewer = tableViewer;
        this.slaveServer = slaveServer;
    }

    /**
     * "启动/关闭"的Action类
     */
    private class OpenAction extends Action {
        public OpenAction() {
            setText(BaseMessages.getString(Spoon.class, "SpoonSlave.QuartzManage.OpenOrClose"));
        }

        public void run() {
            IStructuredSelection selection = (IStructuredSelection)tableViewer.getSelection();
            TaskCronInfo obj = (TaskCronInfo)(selection.getFirstElement());
            if (obj == null) {
                MessageDialog.openInformation(null, null,
                    BaseMessages.getString(Spoon.class, "SpoonSlave.QuartzManage.StartTip"));
            } else {
                try {
                    // UpdateCronTaskServlet
                    String path = "/kettle/updatecrontask";
                    slaveServer.execService(path + "?filename=" + URLEncoder.encode(obj.getTaskFile(), "UTF-8")
                        + "&type=" + obj.getType() + "&enable=" + (!obj.getIsEnable()));
                    obj.setIsEnable(!obj.getIsEnable());
                    tableViewer.refresh();// 调用表格的刷新方法.
                } catch (Exception e) {
                    e.printStackTrace();
                    MessageDialog.openInformation(null, null,
                        BaseMessages.getString(Spoon.class, "SpoonSlave.QuartzManage.UpdateError"));
                }
            }
        }
    }

    /**
     * 停止并删除的Action类
     */
    private final class RefreshAction extends Action {
        public RefreshAction() {
            setText(BaseMessages.getString(Spoon.class, "SpoonSlave.QuartzManage.stopAndDelete"));
        }

        public void run() {
            IStructuredSelection selection = (IStructuredSelection)tableViewer.getSelection();
            TaskCronInfo obj = (TaskCronInfo)(selection.getFirstElement());
            try {
                // UpdateCronTaskServlet
                String path = "/kettle/updatecrontask";
                slaveServer.execService(path + "?filename=" + URLEncoder.encode(obj.getTaskFile(), "UTF-8") + "&type="
                    + "delete" + "&enable=" + obj.getIsEnable());
                obj.setType("delete");
                tableViewer.refresh();// 调用表格的刷新方法.
            } catch (Exception e) {
                e.printStackTrace();
                MessageDialog.openInformation(null, null,
                    BaseMessages.getString(Spoon.class, "SpoonSlave.QuartzManage.UpdateError"));
            }
        }
    }
}